﻿using RFBR.Core;
using RFBR.Core.Helpers;
using RFBR.Core.Infrastructure.Models.Structure;
using RFBR.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace RFBR.Controllers
{
    public class MainMenuController : Controller
    {
        //
        // GET: /MainMenu/
        public ActionResult Index(MainMenu model)
        {
            if (Session[SessionKey.KnowledgeBase] != null)
            {
                ViewBag.KBOpened = true;
            }
            return PartialView("MainMenu", model);
        }

        [HttpGet]
        public void Open()
        {

        }

        [HttpPost]
        public ActionResult Open(MainMenu model)
        {
            IKnowledgeBase kb = KnowledgeBaseHelper.KnowledgeBaseDeserialize(model.KnowledgeBaseFile.InputStream);
            Session[SessionKey.KnowledgeBase] = kb;
            ViewBag.KBOpened = true;
            return RedirectToAction("Index", "KnowledgeBase");
        }

        [HttpGet]
        public FileStreamResult ExportToXml()
        {
            IKnowledgeBase kb = (IKnowledgeBase)Session[SessionKey.KnowledgeBase];
            var output = KnowledgeBaseHelper.KnowledgeBaseSerialize(kb);
            return File(output, "text/xml", kb.Name + ".xml");
        }
    }
}